//
//  HomepageViewController.m
//  Taste
//
//  Created by 徐昊 on 16/8/29.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "HomepageViewController.h"
#import "HXMacro.h"
#import "HomePageTableViewCell.h"
#import "HomePageWebViewTableViewCell.h"
#import "ZWYNetTool.h"
#import "HomepageModel.h"
#import "Res.h"
#import "Content.h"
#import "HXCycleImage.h"
#import "HomePageHeaderView.h"
#import "WebViewController.h"
#import "NSString+Vali.h"
#import "PersonListViewController.h"
#import "DailyInformationViewController.h"
#import "VideoViewController.h"

#define kHomePageTableViewReuseCellName @"HomePageTableViewCellNameReuse"
#define kHomePageTableViewWebViewReuseCellName @"HomePageTableViewWebViewReuseCellName"
#define kHomePageTableViewHeaderViewReuseCellName @"HomePageTableViewHeaderViewReuseCellName"
@interface HomepageViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) HomepageModel *model;
@property (nonatomic, strong) HXCycleImage *headerView;
@property (nonatomic, strong) NSArray *headerTitleArray;
//@property (nonatomic, strong) UIImageView *rotateView;
@end

@implementation HomepageViewController

- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = NO;
    self.navigationController.navigationBarHidden = YES;

}
- (BOOL)prefersStatusBarHidden {
    return YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self prefersStatusBarHidden];
    [self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
    // 设置区头视图标题数组
    [self setArray];
    NSDictionary *dic = @{@"version" : @"2.50",
                          @"Content-Type" : @"application/x-www-form-urlencoded",
                          @"Content-Length" : @"0"};
    [ZWYNetTool POST:@"http://app.legendzest.cn//index3.php?m=api250&c=index&a=index251" andBody:nil andBodyStyle:ZWYBodyJSON andHeader:dic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _model = [HomepageModel modelWithDic:result];
        _headerView.topimg = _model.res[0].content;
        [_tableView reloadData];
    } andFailureBlock:^(NSError *error) {
        NSLog(@"请求失败%@", error);
    }];
    self.navigationController.navigationBarHidden = YES;
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 49)];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView registerNib:[UINib nibWithNibName:@"HomePageTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kHomePageTableViewReuseCellName];
    [_tableView registerNib:[UINib nibWithNibName:@"HomePageWebViewTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kHomePageTableViewWebViewReuseCellName];
    [self.view addSubview:_tableView];
    [_tableView registerClass:[HomePageHeaderView class] forHeaderFooterViewReuseIdentifier:kHomePageTableViewHeaderViewReuseCellName];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    _headerView = [[HXCycleImage alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 0.77 * SCREEN_WIDTH)];
    _headerView.topimg = _model.res[0].content;
    _headerView.backgroundColor = [UIColor cyanColor];
    _tableView.tableHeaderView = _headerView;
//    _rotateView.image = [UIImage imageNamed:@"Img_Loaing_ActivityIndicator_00"];
//    _rotateView.frame = CGRectMake(100, 100, 400, 400);
//    _rotateView.center = self.view.center;
//    _rotateView.backgroundColor = [UIColor redColor];
//    [self.view addSubview:_rotateView];

}
- (void)setArray {
    _headerTitleArray = @[@"秀场推荐", @"每日资讯", @"", @"大师菜谱", @"精选餐厅", @"视频", @"人物志"];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return SCREEN_WIDTH / 3;
    } else if (indexPath.section == 1 || indexPath.section == 4 || indexPath.section ==  6) {
        return SCREEN_WIDTH / 3 + 30;
    } else if (indexPath.section == 2) {
        return SCREEN_WIDTH / 3.04;
    } else {
        return SCREEN_WIDTH / 2 / 1.78 + 30;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 7;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 2) {
        HomePageWebViewTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kHomePageTableViewWebViewReuseCellName];
        if (_model) {
            cell.array = _model.res[indexPath.section + 1].content;
        }
        return cell;
    } else {
        HomePageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kHomePageTableViewReuseCellName];
        if (_model) {
            cell.array = _model.res[indexPath.section + 1].content;
            
            cell.num = indexPath.section;
        }
        return cell;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 2) {
        return nil;
    } else {
        HomePageHeaderView *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:kHomePageTableViewHeaderViewReuseCellName];
        header.title.text = _headerTitleArray[section];
        header.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
#pragma mark - 设置点击tableView区头视图进行跳转
        if (section == 0) {
            [tap addTarget:self action:@selector(show)];
        }
        if (section == 1) {
            [tap addTarget:self action:@selector(dailyInformation)];
        }
        if (section == 3) {
            [tap addTarget:self action:@selector(cookBook)];
        }
        if (section == 4) {
            [tap addTarget:self action:@selector(selection)];
        }
        if (section == 5) {
            [tap addTarget:self action:@selector(pushVideo)];
        }
        if (section == 6) {
            [tap addTarget:self action:@selector(person)];
        }
        [header addGestureRecognizer:tap];
        return header;
    }
}
- (void)show {
    [self.tabBarController setSelectedIndex:2];
}
- (void)dailyInformation {
    DailyInformationViewController *vc = [[DailyInformationViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)cookBook {
    [self.tabBarController setSelectedIndex:3];
}
- (void)selection {
    [self.tabBarController setSelectedIndex:1];
}
- (void)pushVideo {
    VideoViewController *vc = [[VideoViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)person {
    PersonListViewController *vc = [[PersonListViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 2) {
        return 0;
    } else {
        return 40;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 2) {
        WebViewController *webView = [[WebViewController alloc] init];
        webView.urlStr = _model.res[indexPath.section + 1].content[0].url;
        [self.navigationController pushViewController:webView animated:YES];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
